﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using QWEntity.Local;
using QWModel;
using QWRepository;
using QWRepository.Local;
using QWTouchPanel.Properties;

namespace QWTouchPanel.Model
{
    public static class QWPrinting
    {

        /// <summary>
        /// Prints the with CR layout.
        /// </summary>
        /// <param name="nomor">The nomor.</param>
        /// <param name="loket">The loket.</param>
        /// <param name="copies">The copies.</param>
        /// <param name="sender">The sender.</param>
        public static void PrintWithCRLayout(string nomor, string loket, int copies, IWin32Window sender)
        {
            using (var f = new QWaitingWidget("Sedang proses mencetak, tunggu sejenak"))
            {
                // show PROGRESS
                f.Show(sender);
                Application.DoEvents();

                // load REPORT FILES
                var crDocument = new ReportDocument();
                crDocument.Load(Application.StartupPath + "/Reports/PrintNomor.Rpt");
                crDocument.DataDefinition.FormulaFields["nomor"].Text =
                    (char)34 + nomor + (char)34;
                crDocument.DataDefinition.FormulaFields["loket"].Text =
                    (char)34 + loket + (char)34;

                // get company profile
                var db = new CompanyBase();
                List<Company> companies = db.GetAll();
                db.Dispose();

                if (companies != null)
                {
                    crDocument.DataDefinition.FormulaFields["companyname"].Text =
                        (char)34 + companies[0].CompanyName + (char)34;
                    crDocument.DataDefinition.FormulaFields["phone"].Text =
                        (char)34 + companies[0].Phone + (char)34;
                    crDocument.DataDefinition.FormulaFields["email"].Text =
                        (char)34 + companies[0].Email + (char)34;
                }

                try
                {
                    // Get Default Printer Name
                    var pname = new StringBuilder(255);
                    int length = 255;
                    Printer.GetDefaultPrinter(pname, ref length);

                    // start PRINTING
                    crDocument.PrintOptions.PrinterName = pname.ToString().Trim();
                    crDocument.PrintToPrinter(copies, false, 1, 1);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, @"Error !",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Prints the with ESC.
        /// </summary>
        /// <param name="nomor">The nomor.</param>
        /// <param name="loket">The loket.</param>
        /// <param name="copies">The copies.</param>
        /// <param name="sender">The sender.</param>
        public static void PrintWithESC(string nomor, string loket, int copies, IWin32Window sender)
        {
            using (var f = new QWaitingWidget("Sedang proses mencetak, silahkan tunggu !"))
            {
                f.Show(sender);
                Application.DoEvents();

                // get company profile
                var db = new CompanyBase();
                List<Company> companies = db.GetAll();
                db.Dispose();

                var pr = new StringBuilder();

                pr.Append(DirectPrint.InitializePrinter());
                pr.Append(DirectPrint.PrintCenterText() + "SELAMAT DATANG");

                if (companies != null && companies.Count > 0)
                {
                    pr.Append(DirectPrint.FeedPaper(1) +
                              DirectPrint.PrintCenterText() + companies[0].CompanyName);
                }

                pr.Append(DirectPrint.FeedPaper(1));
                pr.Append(DirectPrint.Bold(true));
                pr.Append(DirectPrint.Strong(Settings.Default.StrongSize));
                pr.Append(DirectPrint.FeedPaper(1) + DirectPrint.PrintCenterText() + nomor);
                pr.Append(DirectPrint.Strong(0));
                pr.Append(DirectPrint.Bold(false));
                pr.Append(DirectPrint.FeedPaper(1) + DirectPrint.PrintCenterText() + loket);
                pr.Append(DirectPrint.FeedPaper(2) +
                          DirectPrint.PrintCenterText() + DateTime.Now.ToString("dddd, dd-MM-yyyy"));
                pr.Append("  -  " + DateTime.Now.ToString("HH:mm:ss"));
                pr.Append(DirectPrint.FeedPaper(1) +
                          DirectPrint.PrintCenterText() + "SILAHKAN TUNGGU, TERIMA KASIH");

                pr.Append(DirectPrint.FeedPaper(8));

                if (Settings.Default.AutoCutter)
                {
                    pr.Append(DirectPrint.CutPaper());
                }

                try
                {
                    var pname = new StringBuilder(255);
                    int length = 255;

                    // get default printer name
                    Printer.GetDefaultPrinter(pname, ref length);

                    // loop print by copies
                    for (int i = 1; i <= copies; i++)
                        RawPrinterHelper.SendStringToPrinter(pname.ToString().Trim(), pr.ToString().Trim());

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, @"Error !",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
